package com.evening.dailylife.core.di

import android.content.Context
import com.evening.dailylife.core.data.analytics.TransactionAnalyticsRepository
import com.evening.dailylife.core.data.local.dao.TransactionDao
import com.evening.dailylife.core.data.local.database.AppDatabase
import com.evening.dailylife.core.data.repository.TransactionRepository
import com.evening.dailylife.core.util.StringProvider
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase {
        return AppDatabase.getDatabase(context)
    }

    @Singleton
    @Provides
    fun provideTransactionDao(appDatabase: AppDatabase): TransactionDao {
        return appDatabase.transactionDao()
    }

    @Singleton
    @Provides
    @ApplicationScope
    fun provideApplicationScope(): CoroutineScope {
        return CoroutineScope(SupervisorJob() + Dispatchers.Default)
    }

    @Singleton
    @Provides
    fun provideTransactionRepository(
        transactionDao: TransactionDao,
        @ApplicationScope applicationScope: CoroutineScope
    ): TransactionRepository {
        return TransactionRepository(transactionDao, applicationScope)
    }

    @Singleton
    @Provides
    fun provideTransactionAnalyticsRepository(
        transactionRepository: TransactionRepository,
        stringProvider: StringProvider,
        @ApplicationScope applicationScope: CoroutineScope
    ): TransactionAnalyticsRepository {
        return TransactionAnalyticsRepository(
            transactionRepository = transactionRepository,
            stringProvider = stringProvider,
            applicationScope = applicationScope
        )
    }
}
